package client.control;

//import com.sun.java.util.jar.pack.ConstantPool;
import base.entity.info.FileInfo;
import base.entity.info.Info;
import base.entity.regitem.FileRegItem;
import base.entity.regitem.RegItem;
import base.entity.serach.SearchResult;
import base.service.DirectoriesServer;
import base.service.FileService;
import base.service.impl.DirectoriesServerImpl;
import base.service.impl.FileServiceImpl;
import base.view.DirectoriesView;
import base.view.View;
import client.control.entity.File;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import client.application.Main;
import client.util.StageManager;
import javafx.stage.WindowEvent;

import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

public class IndexController implements Initializable {
    @FXML
    public String url = "";
    public TableView<Info> folderTableView;
    public TableView<FileInfo> fileTableView;
    public ObservableList<File> disk = FXCollections.observableArrayList(
            new File("新加卷（C:）", "文件夹"),
            new File("新加卷（D:）", "文件夹"),
            new File("新加卷（E:）", "文件夹")
    );
    public ObservableList<ObservableList<File>> datas = FXCollections.observableArrayList();
    public ObservableList<Info> folderData;
    public ObservableList<FileInfo> fileData;
    public TableColumn<Info, String> nameTableColumn;
    //public TableColumn<File, String> dateTableColumn;
    public TableColumn<FileInfo, String> typeTableColumn;
    public Label fileCount;
    public Button newFile;
    public MenuBar menuBar;
    public TextField reNameTextField;
    public MenuItem reNameMenuItem;
    public HBox textHBox;
    public AnchorPane textPane;
    public VBox operationVBox;
    public VBox fileOperationVBox;
    public TextField locationTextField;
    public TableColumn fileNameTableColumn;
    public TableColumn fileTypeTableColumn;
    public TextField renameFileTextField;
    public HBox fileTextHBox;
    public Label fileNameNullError;
    public TextField fileNameText;
    public VBox newFileName;
    public TextField searchText;
    public VBox attributeVBox;
    public Label fileNameAttribute;
    public CheckBox onlyRead;
    public CheckBox readAndWrite;

    public String currentPath = "1";
    public Label fileUrl;
    public Label startBolck;
    public Label fileLength;
    public String fileNameProperty;
    public Label jumpError;
    public boolean searching = false;       //判断是否正在搜索，默认值是没有正在搜索
    public VBox createErrorVBox;
    DirectoriesServer directoriesServer = new DirectoriesServerImpl();
    FileService fileService = new FileServiceImpl();

    public void test() {
        System.out.println("here");
    }

    public void show(String url) {
        //文件路径
//        url = "此电脑";
        String historyUrl = url;
        jumpError.setVisible(false);
        try{
            folderData = FXCollections.observableList(directoriesServer.dir(url).getChildDirInfo());
            fileData = FXCollections.observableList(directoriesServer.dir(url).getChildFileInfo());
            this.url = url;
            folderData.removeIf(fileInfo -> "$  ".equals(fileInfo.getName()));
            fileData.removeIf(fileInfo -> "$  ".equals(fileInfo.getName()));
//            System.out.println("删除后的长度："+folderData.size());
//            for (Info info:
//                    folderData) {
//                System.out.println(info.getName());
//                if ("$".equals(info.getName())) {
//                    folderData.remove(info);
//                    System.out.println("删除后的长度："+folderData.size());
//                }
//            }
//            for (Info info :
//                    fileData) {
//                System.out.println(info.getName());
//                if ("$".equals(info.getName())) {
//                    fileData.remove(info);
//                    System.out.println("删除后的长度："+fileData.size());
//                }
//            }
        } catch (IOException e) {
            this.url = historyUrl;
            jumpError.setVisible(true);
            e.printStackTrace();
        }
        folderTableView.setItems(folderData);
        fileTableView.setItems(fileData);

        System.out.println("come show");
        fileCount.setText("---   " + (folderData.size()+fileData.size()) + " 个项目   ---");
    }


    @Override
    public void initialize(URL location, ResourceBundle resources) {

        show(url);
        //FormatDisk formatDisk = new FormatDisk();
        //formatDisk.format();

//        try{
//            folderData = FXCollections.observableList(server.dir("").getChildDirInfo());
//            fileData = FXCollections.observableList(server.dir("").getChildFileInfo());
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        System.out.println("initialize");
        nameTableColumn.setCellValueFactory(
                new PropertyValueFactory<>("name"));
//        dateTableColumn.setCellValueFactory(
//                new PropertyValueFactory<>("date"));
        typeTableColumn.setCellValueFactory(
                new PropertyValueFactory<>("type"));
        folderTableView.setItems(folderData);

        fileNameTableColumn.setCellValueFactory(
                new PropertyValueFactory<>("name"));
//        dateTableColumn.setCellValueFactory(
//                new PropertyValueFactory<>("date"));
        fileTypeTableColumn.setCellValueFactory(
                new PropertyValueFactory<>("type"));
        fileTableView.setItems(fileData);
        folderTableView.setOnMousePressed(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                fileTextHBox.setVisible(false);
                newFileName.setVisible(false);
                fileOperationVBox.setVisible(false);
                createErrorVBox.setVisible(false);
                //这里是双击要进行的操作
                if (event.isPrimaryButtonDown() && event.getClickCount() == 2) {
                   // System.out.println(folderTableView.getSelectionModel().getSelectedItem().getName());
//                    locationTextField.setText(locationTextField.getText() + " > " + folderTableView.getSelectionModel().getSelectedItem().getName());
                    //只要把data改变就可以了
                    //StageManager.DATAS.add(data);
                   // System.out.println(StageManager.DATAS.size());
                    //System.out.println(data);
                    textHBox.setVisible(false);
                    folderTableView.setOpacity(1);
                    openFolder();
                }
                //这里是右键要进行的操作
                else if (event.getButton() == MouseButton.SECONDARY) {
//                    menuBar.setVisible(true);
//                    menuBar.setLayoutY(event.getY());
//                    menuBar.setLayoutX(event.getX());
//                    reNameMenuItem.setText("重命名 " + folderTableView.getSelectionModel().getSelectedItem().getName());
                    if (!searching) {
                        textHBox.setVisible(false);
                        folderTableView.setOpacity(1);
                        operationVBox.setVisible(true);
                        fileOperationVBox.setVisible(false);
                        operationVBox.setLayoutX(event.getX());
                        operationVBox.setLayoutY(event.getY());
                    }
//                    System.out.println(event.getX() + " " + event.getY());
                }
                else {
//                    menuBar.setVisible(false);
                    operationVBox.setVisible(false);
                    textHBox.setVisible(false);
                    folderTableView.setOpacity(1);
                }
            }
        });

        fileTableView.setOnMousePressed(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                operationVBox.setVisible(false);
                createErrorVBox.setVisible(false);
                if (event.isPrimaryButtonDown() && event.getClickCount() == 2) {        //双击进行的操作
                    //System.out.println(fileTableView.getSelectionModel().getSelectedItem().getName());
                    //只要把data改变就可以了
                    //StageManager.DATAS.add(data);
                    // System.out.println(StageManager.DATAS.size());
                    //System.out.println(data);
                    textHBox.setVisible(false);
                    fileTableView.setOpacity(1);
                    try {
                        openFile();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                else if (event.getButton() == MouseButton.SECONDARY) {          //点击右键进行的操作
//                    menuBar.setVisible(true);
//                    menuBar.setLayoutY(event.getY());
//                    menuBar.setLayoutX(event.getX());
//                    reNameMenuItem.setText("重命名 " + folderTableView.getSelectionModel().getSelectedItem().getName());
                    if (!searching) {
                        textHBox.setVisible(false);
                        fileTableView.setOpacity(1);
                        fileOperationVBox.setVisible(true);
                        operationVBox.setVisible(false);
                        fileOperationVBox.setLayoutX(event.getX());
                        fileOperationVBox.setLayoutY(event.getY()+290);
                    }
//                    System.out.println(event.getX() + " " + event.getY());
//                    System.out.println(event.getSceneX() + " " + event.getSceneY());
//                    System.out.println(event.getScreenX() + " " + event.getScreenY());
                }
                else {
//                    menuBar.setVisible(false);
                    fileOperationVBox.setVisible(false);
                    textHBox.setVisible(false);
                    fileTableView.setOpacity(1);
                }
            }
        });

        if(folderData != null && fileData != null) {
            fileCount.setText("---   " + (folderData.size()+fileData.size()) + " 个项目   ---");
        }
    }

    //打开文件
    public void openFile() throws Exception {
        if (searching) {
            url = fileTableView.getSelectionModel().getSelectedItem().getName().substring(8, fileTableView.getSelectionModel().getSelectedItem().getName().length()-5);
            if (url.startsWith("/")) url = fileTableView.getSelectionModel().getSelectedItem().getName().substring(9, fileTableView.getSelectionModel().getSelectedItem().getName().length()-5);
        }
        String name = fileTableView.getSelectionModel().getSelectedItem().getName().substring(fileTableView.getSelectionModel().getSelectedItem().getName().lastIndexOf("/")+1, fileTableView.getSelectionModel().getSelectedItem().getName().length()-1);
        System.out.println("name: ");
        System.out.println("要打开的url: " + url);
        System.out.println("要打开的name：" + name);
        fileOperationVBox.setVisible(false);
        System.out.println(fileTableView.getSelectionModel().getSelectedItem().getName());
//        //传入要打开的文件的名称，打开文件
//        show();
        Stage primaryStage = new Stage();
        Parent root = FXMLLoader.load(getClass().getResource("/client/resource/showFile.fxml"));
        primaryStage.setTitle(url + "." + name);
        primaryStage.getIcons().add(new Image(
                Main.class.getResourceAsStream("/client/resource/image/file package.png")
        ));
        primaryStage.setScene(new Scene(root, 600, 400));
        primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent event) {
                System.out.println("监听到窗口关闭");
                if (searching) {
                    fileService.closeFile(url + "/" + name);
                } else {
                    fileService.closeFile(url + "/" + fileTableView.getSelectionModel().getSelectedItem().getName() + "." + fileTableView.getSelectionModel().getSelectedItem().getType());
                }
            }
        });
        StageManager.STAGE.put("showFile", primaryStage);
        StageManager.CONTROLLER.put("indexControl", this);
        primaryStage.show();
        ShowFileController controller = (ShowFileController) StageManager.CONTROLLER.get("showFileControl");
        if (searching) {
            controller.getFileContent(url, name, fileService);
        }else {
            controller.getFileContent(url, fileTableView.getSelectionModel().getSelectedItem().getName() + "." + fileTableView.getSelectionModel().getSelectedItem().getType(), fileService);
        }
    }

    //打开文件夹
    public void openFolder() {
        searching = false;      //改为不是正在搜索
        operationVBox.setVisible(false);
        searchText.setText("");
        //传入要打开的文件夹的名称，打开文件夹
        //返回这文件夹的子目录及文件的名称及类型
        if (folderTableView.getSelectionModel().getSelectedItem().getName().length() > 3) {
            if ("".equals(url)) url = folderTableView.getSelectionModel().getSelectedItem().getName().substring(9, folderTableView.getSelectionModel().getSelectedItem().getName().length()-1);
            else url = folderTableView.getSelectionModel().getSelectedItem().getName().substring(8, folderTableView.getSelectionModel().getSelectedItem().getName().length()-1);
            locationTextField.setText(url);
            currentPath = url;
            System.out.println("url: " + url);
        } else {
            locationTextField.setText(locationTextField.getText() + " > " + folderTableView.getSelectionModel().getSelectedItem().getName());
            System.out.println(currentPath + folderTableView.getSelectionModel().getSelectedItem().getName());
            currentPath += folderTableView.getSelectionModel().getSelectedItem().getName();
            if(url.equals("")) url += folderTableView.getSelectionModel().getSelectedItem().getName();
            else url = url + "/" + folderTableView.getSelectionModel().getSelectedItem().getName();
            System.out.println("openUrl:" + url);
        }
        show(url);
    }

    //新建新建文件夹的窗口
    public void newFolder() throws Exception {
        operationVBox.setVisible(false);
        Stage primaryStage = new Stage();
        Parent root = FXMLLoader.load(getClass().getResource("/client/resource/newFileName.fxml"));
        primaryStage.setTitle("新建文件");
        primaryStage.getIcons().add(new Image(
                Main.class.getResourceAsStream("/client/resource/image/file package.png")
        ));
        primaryStage.setScene(new Scene(root, 466, 178));
        StageManager.STAGE.put("newFileName", primaryStage);
        StageManager.CONTROLLER.put("indexControl", this);
        primaryStage.show();
    }

    //新建文件夹
    public void addFolder(String folderName) {
//        SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm");//设置日期格式
//        String modifyTime = df.format(new Date());// new Date()为获取当前系统时间

        if(folderName.length() != 3) {
            createErrorVBox.setVisible(true);
        }
        else {
            createErrorVBox.setVisible(false);
            System.out.println(url + folderName);
            try {
                if(url.equals("")) {
                    directoriesServer.md(folderName);
                } else {
                    directoriesServer.md(url + "/" + folderName);
                }
            } catch (IOException e) {
                directoriesServer = new DirectoriesServerImpl();
                e.printStackTrace();
            }
            //传入新建文件夹的名称，新建文件夹
            //返回新建文件夹之后的目录文件序列
            //folderData.add(new File(folderName, "文件夹"));
            show(url);
        }
    }

    //显示文件夹重命名控件
    public void reName() {
        textHBox.setVisible(true);
//        menuBar.setVisible(false);
        reNameTextField.requestFocus();
        operationVBox.setVisible(false);
        folderTableView.setOpacity(0.5);
        System.out.println("reName");
    }

    //删除文件夹
    public void deleteFolder() {
        operationVBox.setVisible(false);
        fileCount.setText("---   " + (folderData.size()+fileData.size()) + " 个项目   ---");
        //传入要删除目录的名称
        //返回删除后的目录文件
        String folderUrl = new String(url);
        try {
            if(url.equals("")) folderUrl +=  folderTableView.getSelectionModel().getSelectedItem().getName();
            else folderUrl += "/" + folderTableView.getSelectionModel().getSelectedItem().getName();
            System.out.println("delete folder url：" + folderUrl);
            directoriesServer.rd(folderUrl);
//            new FormatDisk().show();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            show(url);
        }
    }

    public void cancel() {
//        menuBar.setVisible(false);
        operationVBox.setVisible(false);
    }

    //修改文件夹名字
    public void modifyName() {
//        SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm");//设置日期格式
//        String modifyTime = df.format(new Date());// new Date()为获取当前系统时间
        //folderData.set(folderData.indexOf(folderTableView.getSelectionModel().getSelectedItem()), new File(reNameTextField.getText(), folderTableView.getSelectionModel().getSelectedItem().getType()));
        textHBox.setVisible(false);
        folderTableView.setOpacity(1);
        System.out.println("modifyName");
        System.out.println(folderTableView.getSelectionModel().getSelectedItem().getName() + "---->" + reNameTextField.getText());
        //传入重命名前的名称和命名后的名称
        //返回重命名后的目录文件
        show(url);
    }

    public void backPage() {
        if(StageManager.DATAS.size() > 0) {
            StageManager.DATAS.remove(folderData);
            System.out.println(StageManager.DATAS.get(StageManager.DATAS.size()-1));
            //Collections.copy(data, StageManager.DATAS.get(StageManager.DATAS.size()-1));
        }
    }

    //显示重命名文件控件
    public void renameFile() {
        renameFileTextField.setText("");
        fileTextHBox.setVisible(true);
        renameFileTextField.requestFocus();
        reNameTextField.setText("");
        fileOperationVBox.setVisible(false);
        fileTableView.setOpacity(0.5);
        System.out.println("rename");
    }

    //重命名文件
    public void modifyFileName() {
        fileTextHBox.setVisible(false);
        fileTableView.setOpacity(1);
        System.out.println(fileTableView.getSelectionModel().getSelectedItem().getName() + "------------------------->" + renameFileTextField.getText());
        //传入重命名前的名称和命名后的名称
        //返回重命名后的目录文件
        FileRegItem fileRegItem = fileService.searchFileByPath(url + "/" + fileTableView.getSelectionModel().getSelectedItem().getName() + "." + fileTableView.getSelectionModel().getSelectedItem().getType());
        boolean oR = (fileRegItem.getProperty() & View.ONLY_READABLE) > 0;
        boolean readAndWriteAble = (fileRegItem.getProperty() & View.READABLE_AND_WRITEABLE) > 0;
        //fileService.openFileByPath(url + "/" + fileNameProperty);
        int p = 0;
        if (readAndWriteAble) {
            System.out.println("选择了读写");
            p = p | View.READABLE_AND_WRITEABLE;
        } else if (oR) {
            System.out.println("选择了只读");
            p = p | View.ONLY_READABLE;
        } else {
            System.out.println("都不选默认读写");
            p = p | View.READABLE_AND_WRITEABLE;
        }
        System.out.println("要打开的文件：" + url + "/" + fileTableView.getSelectionModel().getSelectedItem().getName() + "." + fileTableView.getSelectionModel().getSelectedItem().getType());
        System.out.println("fileRegItem : " + fileRegItem);
        fileService.closeFile(url+ "/" + fileTableView.getSelectionModel().getSelectedItem().getName() + "." + fileTableView.getSelectionModel().getSelectedItem().getType());
        fileService.changeFileType(renameFileTextField.getText(), (byte) p, url, fileRegItem);
        show(url);
    }

    //删除文件
    public void deleteFile() {
        fileOperationVBox.setVisible(false);
        System.out.println(fileTableView.getSelectionModel().getSelectedItem().getName());
        //传入要删除的文件的名称
        //返回删除后的目录文件
        try {
            DirectoriesView directoriesView = directoriesServer.dir(url);
            List<RegItem> regItems = directoriesView.getChilds();
            RegItem regItem = null;
            for (RegItem r : regItems){
                if (r.getName().equals(fileTableView.getSelectionModel().getSelectedItem().getName())){
                    regItem = r;
                    break;
                }
            }
            if (regItem == null){
                System.err.println("删除的文件：" + fileTableView.getSelectionModel().getSelectedItem().getName() + "不存在");
                return;
            }
            fileService.closeFile(url+ "/" +fileTableView.getSelectionModel().getSelectedItem().getName()+"."+fileTableView.getSelectionModel().getSelectedItem().getType());
            fileService.deleteFile(url, (FileRegItem) regItem);

        } catch (IOException e) {
            e.printStackTrace();
        }
        show(url);
    }

    //显示新建文件控件
    public void showNewFile() {
        searching = false;      //改为不是正在搜索
        fileNameText.setText("");           //打开前先把新建文件名框设为空
        newFileName.setVisible(true);
        newFileName.setStyle("-fx-background-color:#f2f2f2");
        fileOperationVBox.setVisible(false);
    }

    //新建文件
    public void newFile() {
        String[] inputFileName = fileNameText.getText().split("\\.");
        if(fileNameText.getText().trim().isEmpty()) {
            fileNameNullError.setText("文件名不能为空");
            fileNameNullError.setVisible(true);
        } else if (inputFileName.length==0 && fileNameText.getText().trim().length() > 3) {
            newFileName.setVisible(false);
            fileNameNullError.setText("");
            createErrorVBox.setVisible(true);
        } else {
            if (inputFileName.length == 1) {
                if (inputFileName[0].length() > 3)  {
                    newFileName.setVisible(false);
                    fileNameNullError.setText("");
                    createErrorVBox.setVisible(true);
                } else{
                    newFileName.setVisible(false);
                    fileNameNullError.setText("");
                    System.out.println("新建文件名：" + fileNameText.getText());
                    //传入新建的文件名称以及后缀名
                    //返回 新建文件后的目录文件
                    try {
                        DirectoriesView view = directoriesServer.dir(url);
                        int blockNum = url.equals("") ? 2:view.getItem().getBeginFatNum(); // 当前目录下的文件或目录的登记项所在的盘块好
                        int initialProperty = View.READABLE_AND_WRITEABLE;
                        fileService.creatFile(fileNameText.getText(), initialProperty, blockNum);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    show(url);
                }
            } else if (inputFileName.length == 2) {
                if ((inputFileName[0].length() > 3 || inputFileName[1].length() > 2))  {
                    newFileName.setVisible(false);
                    fileNameNullError.setText("");
                    createErrorVBox.setVisible(true);
                } else{
                    newFileName.setVisible(false);
                    fileNameNullError.setText("");
                    System.out.println("新建文件名：" + fileNameText.getText());
                    //传入新建的文件名称以及后缀名
                    //返回 新建文件后的目录文件
                    try {
                        DirectoriesView view = directoriesServer.dir(url);
                        int blockNum = url.equals("") ? 2:view.getItem().getBeginFatNum(); // 当前目录下的文件或目录的登记项所在的盘块好
                        int initialProperty = View.READABLE_AND_WRITEABLE;
                        fileService.creatFile(fileNameText.getText(), initialProperty, blockNum);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    show(url);
                }
            }
            //fileNameNullError.setVisible(false);
        }
    }

    //查找文件或者文件夹
    public void searchFileOrFolder() {
        if(!searchText.getText().trim().isEmpty()) {
            System.out.println(searchText.getText().trim());
            searchText.setText("");
            //传入搜索的文件名称
            //返回搜索的 目录及文件
            searchText.setText("");
            show(url);
        }
    }

    //返回上一级目录
    public void backToUp() {
        searching = false;      //改为不是正在搜索
//        String[] fileUrl = locationTextField.getText().split(" > ");
//        locationTextField.setText("");
//        if(fileUrl.length != 1){
//            for(int i=0; i<fileUrl.length-1; i++) {
//                if(i==0) {
//                    locationTextField.setText(locationTextField.getText() + fileUrl[i]);
//                } else {
//                    locationTextField.setText(locationTextField.getText() + " > " + fileUrl[i]);
//                }
//            }
//        }
        System.out.println("返回上一级目录");
        //返回上一级目录文件
        if (url.indexOf("/") == -1)url = "";
        else{
            url = url.substring(0,url.lastIndexOf("/"));
        }
        System.out.println("回退到："+url);
        locationTextField.setText(" > 此电脑" + (url.equals("")? "":" > ") + url.replaceAll("/"," > "));
        show(url);
    }

    //返回根目录
    public void returnRoot() {
        searching = false;      //改为不是正在搜索
        locationTextField.setText(" > 此电脑");
        System.out.println("返回根目录");
        //返回根目录 的目录文件
        url = "";
        show(url);
    }

    //显示属性
    public void showAttribute() {
        String fileDetail = url + "/" + fileTableView.getSelectionModel().getSelectedItem().getName()+"."+fileTableView.getSelectionModel().getSelectedItem().getType();
        fileNameProperty = fileTableView.getSelectionModel().getSelectedItem().getName()+"."+fileTableView.getSelectionModel().getSelectedItem().getType();
        FileRegItem fileRegItem = fileService.searchFileByPath(fileDetail);
        int fileLen = fileRegItem.getLength();
        int blockNum = fileRegItem.getBeginFatNum();
        boolean oR = (fileRegItem.getProperty() & View.ONLY_READABLE) > 0;
        boolean readAndWriteAble = (fileRegItem.getProperty() & View.READABLE_AND_WRITEABLE) > 0;

        System.out.println("文件的属性值" + oR + "        " + readAndWriteAble);
        //获取当前目录的地址
        fileUrl.setText(fileDetail);
        //获取文件长度
        fileLength.setText(String.valueOf(fileLen));
        //获取起始盘块号
        startBolck.setText(String.valueOf(blockNum));
        //设置属性
        onlyRead.setSelected(oR);
        readAndWrite.setSelected(readAndWriteAble);
        fileOperationVBox.setVisible(false);
        attributeVBox.setVisible(true);
        attributeVBox.setStyle("-fx-background-color:#f2f2f2");
        fileNameAttribute.setText(fileNameProperty + " 的属性");
    }

    //确认属性
    public void sureAttribute() {
        attributeVBox.setVisible(false);
        int p = 0;
        if (readAndWrite.isSelected()) {
            System.out.println("选择了读写");
            p = p | View.READABLE_AND_WRITEABLE;
        } else if (onlyRead.isSelected()) {
            System.out.println("选择了只读");
            p = p | View.ONLY_READABLE;
        } else {
            System.out.println("都不选默认读写");
            p = p | View.READABLE_AND_WRITEABLE;
        }
        System.out.println(p);
        FileRegItem fileRegItem = fileService.searchFileByPath(url + "/" + fileNameProperty);
        //fileService.openFileByPath(url + "/" + fileNameProperty);
        System.out.println("要修改属性的文件："+ fileNameProperty);
        fileService.changeFileType(fileNameProperty, (byte) p, url, fileRegItem);
        fileRegItem = fileService.searchFileByPath(url + "/" + fileNameProperty);
        System.out.println("修改后："+fileRegItem.getProperty());
        show(url);
    }

    //在路径显示框按回车跳转到相应的目录
    public void directJump(ActionEvent actionEvent) {
        searching = false;      //改为不是正在搜索
        System.out.println(locationTextField.getText());
        show(locationTextField.getText());
//        locationTextField.setOnKeyPressed(new EventHandler<KeyEvent>()
//        {
//            @Override
//            public void handle(KeyEvent key)
//            {
//                if (key.getCode().equals(KeyCode.ENTER))
//                {
//                    System.out.println(locationTextField.getText());
//                }
//            }
//        });
    }

    //搜索文件和文件夹
    public void searchFileAndFolder() {
        try {
            System.out.println("url: " + url);
            System.out.println("搜索的内容： " + searchText.getText());
            DirectoriesView directoriesView = directoriesServer.dir(url);
            List<SearchResult> list = directoriesView.doSearch(searchText.getText().trim());
            System.out.println("---------------------");
            for (SearchResult result:list)System.out.println(result.getPath() + "    "+ result.getName());
            System.out.println("---------------------");
            folderData = FXCollections.observableList(directoriesView.serach2DirectoriesInfo(list));
            fileData = FXCollections.observableList(directoriesView.serach2FileInfo(list));
            fileCount.setText("---   " + (folderData.size()+fileData.size()) + " 个项目   ---");
            folderTableView.setItems(folderData);
            fileTableView.setItems(fileData);
            searching = true;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //确认错误提示
    public void sureError() {
        createErrorVBox.setVisible(false);
    }
}
